<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 16:16
 */

namespace app\api\controller\v9;

use app\common\model\AppChannelPackage;
use app\common\model\AppVersion;
use think\Env;

/**
 * @title App版本更新接口，客户端根据version_code进行是否升级的判断
 * @desc 【附带 控制星盘数据来源为 客户端本地获取或服务器获取 支付方式控制】
 * @url /api/v4/astro_update
 * @version 1.4.0
 * Class AppVersionUpdate
 * @package app\api\controller\v4
 */
class AppVersionUpdate
{
    /**
     * @title 获取app版本更新信息,根据app型号和版本,若开启强制更新则返回渠道包号
     * @method GET
     * @desc 是否有新版本发布 || 星盘获取方式有变化
     * @return void
     * @throws \Exception
     */
    public function index()
    {
        $headers = request()->header();
        $version_code = input('param.version-code');
        $channel = input('param.channel');

        $AppChannelModel = new AppChannelPackage();
        /**
         * 获取当前客户端的最新版本的更新信息
         */
        // $AppVersion = (new AppVersion())->getVersionData($headers['app-type']);
        $AppVersion = (new \app\common\cachemodel\AppVersion())->getVersionData(['type' => $headers['app-type'], 'version' => ($headers['version'] ?? '')]);

        if ($AppVersion)
        {
            $array = [
                'version' => $AppVersion['version'],
                'version_name' => $AppVersion['version_name'],
                'update_content' => $AppVersion['update_content'],
                'force' => $AppVersion['force'],
                'is_astro_data_from_local' => $AppVersion['is_astro_data_from_local'],
                'channel_link' => '',
                'is_hot_update' => config('app.is_hot_update'),
                'pay_type_data' => '2,1',
                'jd_pay_des' => '',
            ];
            $default_pay_type = 2;
            switch (strtolower($headers['app-type']))
            {
                case 'ios':
//                    /**
//                     * 获取苹果支付商品
//                     */
//                    $apple_goods = config('apple_pay_goods');
//
//                    /**
//                     * 苹果支付开启且版本相同ios开启的支付方式
//                     */
//                    if ($apple_goods['switch'] && $headers['version'] == $apple_goods['version'] && strcasecmp($headers['app-type'], 'ios') == 0)
//                    {
//                        $array['pay_type_data'] = '9';
//                    }

                    /**
                     * 获取ios支付方式
                     */
                    $apple_pay = explode(',', $AppVersion['goods_pay_type']);
                    if (in_array($default_pay_type, $apple_pay))
                    {
                        array_unshift($apple_pay, $default_pay_type);
                    }
                    $array['pay_type_data'] = implode(',', array_unique($apple_pay));
                    break;
                case 'android':
                    /**
                     * 判断当前app版本是否需要强制更新,指定更新开启且当前版本包含在内
                     */
                    $AppVersionCodeIds = empty($AppVersion['version_force_data']) ? [] : json_decode($AppVersion['version_force_data'], true);
                    if ($AppVersion['version_force'] == 1 && in_array($version_code, $AppVersionCodeIds))
                    {
                        $array['force'] = 1;
                    }
                    /**
                     * 仅针对安卓1.9.0版本开启热更新
                     */
                    if (strcasecmp($headers['app-type'], 'android') == 0 && strcasecmp($headers['version'], '1.9.0') == 0)
                    {
                        $array['is_hot_update'] = 1;
                    }
                    /**
                     * 仅针对安卓开app内更新更新,返回渠道包
                     */
                    if (strcasecmp('V' . $headers['version'], $AppVersion['version_name']) == 0)
                    {
                        //若app内更开启,返回app渠道包链接
                        if ($AppVersion['app_update'] == 1)
                        {
                            //获取渠道包
                            $channel_link = $AppChannelModel->getChannel($version_code, $channel);
                            $array['channel_link'] = Env::get('host.photo') . $channel_link['channel_link'];
                        }
                    }
                    break;
            }
            return show(config('code.success'), '加载成功', $array);
        }
        return show(config('code.error'), 'app更新信息获取失败');

    }
}
